package com.app.controller.authority;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.app.controller.BaseController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.app.entity.authority.Department;
import com.app.service.authority.DepartmentService;

@RestController
@RequestMapping("/api/depart")
public class DepartmentController extends BaseController {
	
private static final Logger log = LoggerFactory.getLogger(DepartmentController.class);
	
	@Autowired
	private DepartmentService departmentService;



	/**

	 * @return
	 */
	@RequestMapping(value="/delInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delDepart(@RequestBody Department _department){
		Map<String, Object> result=new HashMap<String, Object>();

		if(_department.getId() == null || _department.getId()==""  ){
			result.put("code",400);
			result.put("message", "部门编号错误！操作失败！请联系管理员！");
			result.put("data","-1");
			return result;
		}

		try {
			String threadName = Thread.currentThread().getStackTrace()[1].getMethodName();


			List<Department> list =  this.departmentService.queryDepartByParentId(_department.getId());

			if(list.size()<1){
				Integer resultcount=this.departmentService.delDepart(_department);
				if (resultcount>0) {
					result.put("code",200);
					result.put("message", "操作成功！");
					result.put("data",_department);

				}
				else {
					result.put("code",201);
					result.put("message", "操作失败！请联系管理员！");
					result.put("data","-1");
				}
			}else{
				result.put("code",201);
				result.put("message", "存在下级部门，不能删除！");
				result.put("data",_department);
			}
//			MDC.put(LogConstants.APPID, AppSystems.MP.name());// 当前系统id
//			MDC.put(LogConstants.LOGIN_NAME, "p-yangjinsong");// 当前登录人
//			MDC.put(LogConstants.METHOD_NAME, Thread.currentThread().getStackTrace()[1].getMethodName());// 当前方法名
//			MDC.put(LogConstants.INPUT_PARAM, input.toString());// 入参；如果存入对象，请务必重写了该对象的toString()方法；
//			MDC.put(LogConstants.OUTPUT_PARAM, output.toString());// 出参；如果存入对象，请务必重写了该对象的toString()方法；
//
//			log.info(LogConstants.LOG_OF_ACTION);// 向日志文件和kafka写入MDC内的日志,日志类型为Action；

			return result;

		} catch (Exception e) {
			log.error("func:updateDepart %n_department:"+_department+"%n"+e.getMessage());
			result.put("code", 500);
			result.put("message", e.getMessage());


			return result;
		}


	}


	/**

	 * @return
	 */
	@RequestMapping(value="/modifyInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateDepart(@RequestBody Department _department){

		if(_department.getParentId() == null || _department.getParentId().length()<=0){
			_department.setParentId("0");
		}

		Map<String, Object> result=new HashMap<String, Object>();
		try {

			String threadName = Thread.currentThread().getStackTrace()[1].getMethodName();


			Integer resultcount=this.departmentService.updateDepart(_department);

			if (resultcount>0) {
				result.put("code",200);
				result.put("message", "操作成功！");
				result.put("data",_department);

			}
			else {
				result.put("code",201);
				result.put("message", "机构编码重复，请选择其他机构编号！");
				result.put("data","-1");

			}

			return result;
		} catch (Exception e) {
			log.error("func:updateDepart %n_department:"+_department+"%n"+e.getMessage());
			result.put("code", 500);
			result.put("message", e.getMessage());

			return result;
		}
	}

	/**

	 * @return
	 */
	@RequestMapping(value="/addInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertDepartment(@RequestBody Department _department){

	//	if(_department.getId() == null || _department.getId().length() <= 0);{
			_department.setId(java.util.UUID.randomUUID().toString());
	//	}
		if(_department.getParentId() == null || _department.getParentId().length()<=0){
			_department.setParentId("0");
		}

		Map<String, Object> result=new HashMap<String, Object>();
		try {

			String threadName = Thread.currentThread().getStackTrace()[1].getMethodName();


			Integer resultcount=this.departmentService.insertDepartment(_department);

			if (resultcount>0) {
				result.put("code",200);
				result.put("message", "操作成功！");
				result.put("data",_department);

			}
			else {
				result.put("code",201);
				result.put("message", "机构编码重复，请选择其他机构编号！");
				result.put("data","-1");
			}

			return result;


		} catch (Exception e) {
			log.error("func:insertDepartment %n_department:"+_department+"%n"+e.getMessage());
			result.put("code", 500);
			result.put("message", e.getMessage());

			return result;
		}
	}
//
//	 /***
//	  * ����һ������
//	  * @param department
//	  * @return
//	  */
//	 @RequestMapping(value="/deleteDepartment" ,method=RequestMethod.POST)
//	 public Map<String, Object> deleteDepartment(@RequestBody Department department){
//		Map<String, Object> result=new HashMap<String, Object>();
//
//
//		Department dept=this.departmentService.deleteDepartment(department);
//		try {
//			if (dept!=null) {
//				result.put("code",200);
//				result.put("department", "���ݲ���ɹ�");
//				return result;
//			}
//			result.put("code", 400);
//			result.put("message", "û�в鵽����");
//			return result;
//		} catch (Exception e) {
//			result.put("code", 500);
//			result.put("message", e);
//			return result;
//		}
//
//	}
//
//
//
//	 /***
//	  * ɾ��
//	  * @param department
//	  * @return
//	  */
//	 @RequestMapping(value="/updateDepartment",method=RequestMethod.POST)
//	 public Map<String, Object> updateDepartment(@RequestParam("department")Department department){
//		Map<String, Object> result=new HashMap<String, Object>();
//
//
//		Department dept=this.departmentService.updateDepartment(department);
//		try {
//			if (dept!=null) {
//				result.put("code",200);
//				result.put("department", "���ݲ���ɹ�");
//				return result;
//			}
//			result.put("code", 400);
//			result.put("MSG", "û�в鵽����");
//			return result;
//		} catch (Exception e) {
//			result.put("code", 500);
//			result.put("message", e);
//			return result;
//		}
//
//	}

	 @RequestMapping(value="/getInfoAll",method=RequestMethod.POST)
	 @ResponseBody
	 public Map<String, Object> queryDepartment(@RequestBody Department _department){
		Map<String, Object> result=new HashMap<String, Object>();
		try {
			String threadName = Thread.currentThread().getStackTrace()[1].getMethodName();


			List<Department> list=this.departmentService.queryDepartAll(_department.getId());

			result.put("code",200);
			result.put("message", "succ");
			result.put("data", list);


			return result;
		} catch (Exception e) {
			log.error("func:queryDepartment %parntId:"+_department.getId()+"%n"+e.getMessage());
			result.put("code", 500);
			result.put("message", e);


			return result;
		}

	}
	@RequestMapping(value="/validateCode",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> validateCode(@RequestBody Department _department){
		Map<String, Object> result=new HashMap<String, Object>();
		try {
			String threadName = Thread.currentThread().getStackTrace()[1].getMethodName();


			List<Department> list=this.departmentService.validateLevelCode(_department);

			if(list.size()>0){
				result.put("code",201);
				result.put("message", "编码存在！请选择其他编码");
			}
			else{
				result.put("code",200);
				result.put("message", "succ");
			}


		//	result.put("data", list);


			return result;
		} catch (Exception e) {
			log.error("func:validateCode %_department:"+_department+"%n"+e.getMessage());
			result.put("code", 500);
			result.put("message", e);


			return result;
		}

	}


	@RequestMapping(value="/getInfoByParentId",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryDepartmentByParentId(@RequestBody Department _department){
		Map<String, Object> result=new HashMap<String, Object>();

		try {
			String parentId = _department.getParentId();


			List<Department> depts=this.departmentService.queryDepartByParentId(parentId);
			if (depts!=null) {
				result.put("code",200);
				result.put("message", "succ");
				result.put("data", depts);

			}
			else {
				result.put("code", 201);
				result.put("message", "notexists");
			}


			return result;

		} catch (Exception e) {
			log.error("func:queryDepartmentByParentId %_department:"+_department+"%n"+e.getMessage());
			result.put("code", 500);
			result.put("message", e);

			return result;
		}

	}

	@RequestMapping(value = "/getInfoById", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> selectMenuById(@RequestBody Department _department) {
		Map<String, Object> results = new HashMap<>();
		try {
			List<Department> department = departmentService.getInfoById(_department.getId());
			if (department != null && department.size()>0) {
				results.put("code", 200);
				results.put("message", "操作成功！");
				results.put("data", department.get(0));
			}else if(department != null && department.size()==0){
				results.put("code", 200);
				results.put("message", "操作成功！");
				results.put("data", new Department());
			}


			else {
				results.put("code", 201);
				results.put("message", "操作失败！");
				results.put("data", null);
			}
		} catch (Exception e) {
			results.put("code", 500);
			results.put("message", e);
		}
		return results;
	}
}
